import UIKit

/**
 B站: https://www.bilibili.com/video/BV144411C7Gg?p=11
 
 第11课内容
 
    数组
 
    1. 定义
    2. 增删改查
    3. 遍历
    4. 排序
    5. 过滤
    6. 数组比较
 */

// 数组定义
let a = [1, 2, 3, 4, 5]
print(a)
print(a[2])

let b:[String] = ["hello", "world"]
print(b)

let c:Array<Double> = [1.4, 1.6]
print(c)

let d:Array<Int> = [] // 空数组
print(d)

let e = [Int]() // 空数组、初始化器
print(e)

let f = [1, "hello"] as [Any]
print(f)

let g = Array(repeating: -1, count: 3) // 初始化器
print(g)

let h = Array(repeating: "", count: 5) // 初始化器
print(h)

let j:[Any] = ["0", 1, "2"]
print(j)
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
print("-------数组 查找--------")
var array = ["hello", "world", "Swift"]
print("1", array[0])

// 【注意】怎么，写法不对??????
//print(array.index(0, offsetBy: 1))


print("2 包含", array.contains("Swift"))

// 数组 查找某个元素 的 索引
let lookIndex = array.lastIndex(of: "Swift")
// 方法1
if (lookIndex != nil) {
    print("3-1 找到啦=", lookIndex!)
}
else {
    print("3-1 没有找到 字符串=")
}

// 方法2
if let index = lookIndex {
    print("3-2", index)
}

//----------------------------------------------
print("-------数组 增/添加--------")
array = array + ["0", "1"]
print("1", array)

array.append("srx")
print("2", array)

//array.insert(T##newElement: String##String, at: T##Int)
array.insert("cc", at: 2)
print("3", array)

//----------------------------------------------
print("-------数组 修改--------")
array[0] = "bb"
print("1", array)

array.replaceSubrange((0...1), with: ["dd"])
print("2", array)

//----------------------------------------------
print("-------数组 删除--------")
array.removeFirst()
print("1", array)

array.removeLast()
print("2", array)

array.remove(at: 2)
print("3", array)

array.removeSubrange( (0..<1) )
print("4", array)

array.removeSubrange( (0...1) )
print("5", array)
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
array = ["a", "b", "c", "d"]
print("-------3 数组 遍历--------")
for item in array {
    print(item)
}

print("----")

for item in array[0...(array.count-2)] {
    print("aa--", item)
}

for item in array[0...] {
    print("aa--", item)
}

print("----")

for index in 0..<array.count {
    let item = array[index]
    print(index, item)
}

for index in (0..<array.count).reversed() {
    let item = array[index]
    print(index, item)
}

//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
print("-------3 数组 排序--------")
array.sort { (s1, s2) -> Bool in
    if (s1 > s2) {
        // 倒叙
        return true
    }
    return false
}
print(array)


// 过滤
let a2 = array.filter { (item) -> Bool in
    if (item != "d") {
        return true
    }
    return false
}
print("数组-过滤-1", a2)
print("数组-过滤-2", array)


// 比较
let array1 = [1, 2]
//let array2 = [1, 2, 3] // 可切换方便 被注释掉了
let array2 = [2, 1] // 可切换方便 被注释掉了
if (array1 == array2) {
    print("数组比较 ==")
}
else {
    print("数组比较 不等")
}

/**
数组 比较，
 1. 顺序
 2. 元素
 */
